﻿using Common.Communal;
using ERP.Domain.Products;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using OfficeOpenXml;
using OfficeOpenXml.Style;
using Product.API.Read.Applications.Commands.Products;
using Repository.communal;
using System.Drawing;
using System.Threading.Tasks;

namespace Product.API.Read.Controllers
{
    /// <summary>
    /// 产品API-读
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class ProductController : ControllerBase
    {
        /// <summary>
        /// 中介者
        /// </summary>
        private readonly IMediator mediator;
        private readonly IBaseRepository<ERP.Domain.Products.Product> productRepository;
        private readonly ILogger<ProductController> logger;

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="mediator">中介者</param>
        public ProductController(IMediator mediator, IBaseRepository<ERP.Domain.Products.Product> productRepository, ILogger<ProductController> logger)
        {
            this.mediator = mediator;
            this.productRepository = productRepository;
            this.logger = logger;
        }

        /// <summary>
        /// 产品显示
        /// </summary>
        /// <param name="command">请求</param>
        /// <returns>返回查询的数据</returns>
        [HttpGet("ProductShow")]
        public Task<ApiResult<ApiPaging<ERP.Domain.Products.Product>>> ProductShow([FromQuery] ProductShowCommand command)
        {
            logger.LogInformation("贺治搏想骑马");
            return mediator.Send(command);
        }

        /// <summary>
        /// 产品主键查询
        /// </summary>
        /// <param name="Id">主键</param>
        /// <returns>返回单条</returns>
        [HttpGet("GetProductById")]
        public Task<ApiResult<ERP.Domain.Products.Product>> GetProductById(int Id)
        {
            GetProductByIdCommand command = new GetProductByIdCommand();
            command.Id = Id;
            return mediator.Send(command);
        }


        /// <summary>
        /// 产品导出
        /// </summary>
        /// <returns>返回</returns>
        [HttpGet("ExportExcelProduct")]
        public async Task<IActionResult> ExportExcelProduct()
        {
            List<ERP.Domain.Products.Product> products = await productRepository.GetAllAsync();

            using (var package = new ExcelPackage())
            {
                var worksheet = package.Workbook.Worksheets.Add("产品列表");

                // 设置表头
                worksheet.Cells[1, 1].Value = "产品编号";
                worksheet.Cells[1, 2].Value = "产品名称";
                worksheet.Cells[1, 3].Value = "规格型号";
                worksheet.Cells[1, 4].Value = "单位";
                worksheet.Cells[1, 5].Value = "产品类型";
                worksheet.Cells[1, 6].Value = "产品属性";
                worksheet.Cells[1, 7].Value = "产品分类";
                worksheet.Cells[1, 8].Value = "启用状态";
                worksheet.Cells[1, 9].Value = "有效日期";
                worksheet.Cells[1, 10].Value = "报警天数";
                worksheet.Cells[1, 11].Value = "库存上限";
                worksheet.Cells[1, 12].Value = "库存下限";
                worksheet.Cells[1, 13].Value = "采购价格";
                worksheet.Cells[1, 14].Value = "销售价格";

                // 表头样式设置
                using (var headerRange = worksheet.Cells[1, 1, 1, 14])
                {
                    headerRange.Style.Font.Bold = true;
                    headerRange.Style.Fill.PatternType = ExcelFillStyle.Solid;
                    headerRange.Style.Fill.BackgroundColor.SetColor(Color.LightGray);
                    headerRange.Style.Border.BorderAround(ExcelBorderStyle.Thin);
                }

                // 填充数据
                for (int i = 0; i < products.Count(); i++)
                {
                    int row = i + 2;
                    worksheet.Cells[row, 1].Value = products[i].ProductNumber;
                    worksheet.Cells[row, 2].Value = products[i].ProductName;
                    worksheet.Cells[row, 3].Value = products[i].SpecificationModel;
                    worksheet.Cells[row, 4].Value = products[i].Unit;
                    worksheet.Cells[row, 5].Value = products[i].ProductType;
                    worksheet.Cells[row, 6].Value = products[i].ProductAttribute;
                    worksheet.Cells[row, 7].Value = products[i].ProductClassification;
                    worksheet.Cells[row, 8].Value = products[i].ActivationStatus;
                    worksheet.Cells[row, 9].Value = products[i].ValidDate;
                    worksheet.Cells[row, 10].Value = products[i].AlarmDays;
                    worksheet.Cells[row, 11].Value = products[i].MaximumInventoryLevel;
                    worksheet.Cells[row, 12].Value = products[i].MinimumInventoryLevel;
                    worksheet.Cells[row, 13].Value = products[i].PurchasePrice;
                    worksheet.Cells[row, 14].Value = products[i].SalesPrice;
                    worksheet.Cells[row, 13].Style.Numberformat.Format = "¥#,##0.00";
                    worksheet.Cells[row, 14].Style.Numberformat.Format = "¥#,##0.00";
                }
                // 自动调整列宽
                worksheet.Cells.AutoFitColumns(0);

                // 直接返回文件供下载
                return File(
                    fileContents: package.GetAsByteArray(),
                    contentType: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
                    fileDownloadName: $"产品列表_{DateTime.Now:yyyyMMdd}.xlsx"
                );
            }
        }



    }
}
